package com.tc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tc.dao.DriverAddressMapper;
import com.tc.pojo.DriverAddress;
import com.tc.service.DriverAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/****
 * @Author:admin
 * @Description:DriverAddress业务层接口实现类
 * @Date 2019/6/14 0:16
 *****/
@Service
public class DriverAddressServiceImpl implements DriverAddressService {

    @Autowired
    private DriverAddressMapper driverAddressMapper;


    /**
     * DriverAddress条件+分页查询
     * @param driverAddress 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<DriverAddress> findPage(DriverAddress driverAddress, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        Example example = createExample(driverAddress);
        //执行搜索
        return new PageInfo<DriverAddress>(driverAddressMapper.selectByExample(example));
    }

    /**
     * DriverAddress分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<DriverAddress> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<DriverAddress>(driverAddressMapper.selectAll());
    }

    /**
     * DriverAddress条件查询
     * @param driverAddress
     * @return
     */
    @Override
    public List<DriverAddress> findList(DriverAddress driverAddress){
        //构建查询条件
        Example example = createExample(driverAddress);
        //根据构建的条件查询数据
        return driverAddressMapper.selectByExample(example);
    }


    /**
     * DriverAddress构建查询对象
     * @param driverAddress
     * @return
     */
    public Example createExample(DriverAddress driverAddress){
        Example example=new Example(DriverAddress.class);
        Example.Criteria criteria = example.createCriteria();
        if(driverAddress!=null){
            // 
            if(!StringUtils.isEmpty(driverAddress.getId())){
                    criteria.andEqualTo("id",driverAddress.getId());
            }
            // 
            if(!StringUtils.isEmpty(driverAddress.getDidxy())){
                    criteria.andEqualTo("didxy",driverAddress.getDidxy());
            }
            // 车型id关联货车表
            if(!StringUtils.isEmpty(driverAddress.getTruckId())){
                    criteria.andEqualTo("truckId",driverAddress.getTruckId());
            }
            // 司机名字
            if(!StringUtils.isEmpty(driverAddress.getDrivername())){
                    criteria.andLike("drivername","%"+driverAddress.getDrivername()+"%");
            }
            // 车牌
            if(!StringUtils.isEmpty(driverAddress.getCarno())){
                    criteria.andEqualTo("carno",driverAddress.getCarno());
            }
            // 星级
            if(!StringUtils.isEmpty(driverAddress.getLevel())){
                    criteria.andEqualTo("level",driverAddress.getLevel());
            }
            // 好评度
            if(!StringUtils.isEmpty(driverAddress.getPraise())){
                    criteria.andEqualTo("Praise",driverAddress.getPraise());
            }
        }
        return example;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Integer id){
        driverAddressMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改DriverAddress
     * @param driverAddress
     */
    @Override
    public void update(DriverAddress driverAddress){
        driverAddressMapper.updateByPrimaryKey(driverAddress);
    }

    /**
     * 增加DriverAddress
     * @param driverAddress
     */
    @Override
    public void add(DriverAddress driverAddress){
        driverAddressMapper.insert(driverAddress);
    }

    /**
     * 根据ID查询DriverAddress
     * @param id
     * @return
     */
    @Override
    public DriverAddress findById(Integer id){
        return  driverAddressMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询DriverAddress全部数据
     * @return
     */
    @Override
    public List<DriverAddress> findAll() {
        return driverAddressMapper.selectAll();
    }
}
